package com.example.security.service.impl;

import com.example.entity.User;
import com.example.security.service.UserDetailsImpl;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserService baseUsersService;
    /**
     * 重写loadUserByUsername方法
     * @param username the username identifying the user whose data is required.
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User baseUsers = new User();
        baseUsers.setUsername(username);
        List<User> baseUsersList = baseUsersService.getUserList(baseUsers);
        if (baseUsersList == null || baseUsersList.isEmpty()) {
            System.out.println("------------->       loadUserByUsername验证失败， "+baseUsers.getUsername()+" 不存在！");
            throw new UsernameNotFoundException(username);
        }
        return new UserDetailsImpl(baseUsersList.get(0));	// UserDetailsImpl 是我们实现的类
    }
}
